import Toast from '@vant/weapp/toast/toast'
Page({
  data: {
    userInfo: {},
    imgList: []
  },
  getImgSrc () {
    wx.request({
      method: 'GET',
      url: 'https://image.so.com/j',
      data: {
        q: '尧顺宇',
        src: 'srp',
        correct: '尧顺宇',
        sn: 0,
        pn: 10
      },
      success: res => {
        let tem = []
        res.data.list.map(v => {
          if (/pics0.baidu.com/.test(v.img)) {
            v.img = 'https://images.weserv.nl/?url=' + v.img
          }
          tem.push(v.img)
        })
        this.setData({
          imgList: tem
        })
      },
      fail: err => {
        Toast.fail('失败：' + err)
      }
    })
  },
  imgPreview (e) {
    wx.previewImage({
      urls: this.data.imgList,
      current: e.currentTarget.dataset.imgsrc,
      success: () => {
        wx.cloud.callFunction({
          name: 'startLogin',
          data: {
            add: 1,
            date: Date.now(),
            openid: this.data.userInfo.openid,
            imgSrc: e.currentTarget.dataset.imgsrc
          },
          success: res => {
          },
          fail: err => {
            Toast.fail('失败：' + err)
          }
        })
      }
    })
  },
  onShow: function () {
    this.getTabBar().init()
    const ui = wx.getStorageSync('userinfo')
    this.setData({
      userInfo: ui
    })
    this.getImgSrc()
  }
})
